package pers.myz.controller;


import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import pers.myz.common.QueryPageParam;
import pers.myz.common.Result;
import pers.myz.entity.SnsBlog;
import pers.myz.entity.SnsFriend;
import pers.myz.entity.SnsUser;
import pers.myz.service.SnsBlogService;
import pers.myz.service.SnsFriendService;

import java.util.HashMap;
import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author lzw
 * @since 2023-07-01
 */
@RestController
@RequestMapping("/sns-friend")
public class SnsFriendController {

    @Autowired
    private SnsFriendService snsFriendService;

    @GetMapping("/ifFriend")
    public Result ifFriend(@RequestParam Integer mainId,@RequestParam Integer friendId){
        List list = snsFriendService.lambdaQuery().eq(SnsFriend::getMainId,mainId).eq(SnsFriend::getFriendId,friendId).list();
        if(list.size()>0){
            SnsFriend friend1 = (SnsFriend)list.get(0);
            HashMap res = new HashMap();
            res.put("friend",friend1);
            return Result.suc(res);
        }else{
            return Result.fail();
        }
    }
    //新增
    @PostMapping("/save")
    public Result save(@RequestBody SnsFriend friend){
        return snsFriendService.save(friend)?Result.suc():Result.fail();
    }

    //删除
    @GetMapping("/del")
    public Result del(@RequestParam String id){
        return snsFriendService.removeById(id)?Result.suc():Result.fail();
    }

    @PostMapping("/listPage")
    public Result listPage(@RequestBody QueryPageParam query){
        HashMap param = query.getParam();
        String mainId = (String)param.get("mainId");
        String userName = (String)param.get("userName");
        String gender = (String)param.get("gender");

        Page<SnsFriend> page = new Page();
        page.setCurrent(query.getPageNum());
        page.setSize(query.getPageSize());

        LambdaQueryWrapper<SnsFriend> lambdaQueryWrapper = new LambdaQueryWrapper();
        lambdaQueryWrapper.orderByDesc(SnsFriend::getLastTime);
        if(StringUtils.isNotBlank(mainId) && !"null".equals(mainId)) {
            lambdaQueryWrapper.like(SnsFriend::getMainId, mainId);
        }
        if(StringUtils.isNotBlank(userName)){
            lambdaQueryWrapper.eq(SnsFriend::getUserName,userName);
        }
        if(StringUtils.isNotBlank(gender)){
            lambdaQueryWrapper.eq(SnsFriend::getGender,gender);
        }
        IPage result = snsFriendService.pageC(page,lambdaQueryWrapper);
        return Result.suc(result.getRecords(),result.getTotal());
    }

}
